package cn.tedu.carehome.foundation.controller;

import cn.tedu.carehome.foundation.pojo.dto.*;
import cn.tedu.carehome.foundation.pojo.vo.FoodAdminVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.OlderStaffListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.OlderStaffStandardVO;
import cn.tedu.carehome.foundation.service.IOlderStaffService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/older-staff-links")
@Api(tags = "老人与员工绑定管理模块")
public class OlderStaffLinkController {
    @Autowired
    IOlderStaffService olderStaffService;

    @PostMapping("/add-new")
    @ApiOperation("添加老人员工关联")
    @PreAuthorize("hasAuthority('staff/add-new')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew( OlderStaffAddDTO olderStaffAddDTO) {
        log.debug("开始处理【添加老人员工关联信息】的请求，参数：{}", olderStaffAddDTO);
        olderStaffService.insert(olderStaffAddDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除关联信息")
    @PreAuthorize("hasAuthority('staff/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "关联", required = true, example = "3", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000000, message = "删除关联信息失败，id非法")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除关联信息】的请求，参数：{}", id);
        olderStaffService.deleteByLinkId(id);
        return JsonResult.ok();
    }


    @PostMapping("/update")
    @ApiOperation("修改关联")
    @PreAuthorize("hasAuthority('staff/update')")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> update(@Valid OlderStaffUpdateDTO olderStaffUpdateDTO) {
        log.debug("开始处理修改关联请求,参数:{}",olderStaffUpdateDTO);
        olderStaffService.updateLink(olderStaffUpdateDTO);
        return JsonResult.ok();
    }


    @GetMapping("/{id:[0-9]+}")
    @ApiOperation(value="根据id请求关联信息")
    @PreAuthorize("hasAuthority('staff/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "关联ID", required = true, example = "9527", dataType = "long")
    public JsonResult<OlderStaffStandardVO> selectById(@Range(min = 1, max = 100000, message = "查询关联失败，ID非法！")
                                                 @PathVariable Long id){
        log.debug("开始处理[根据id查询关联],参数:{}",id);
        OlderStaffStandardVO olderStaffStandardVO=olderStaffService.selectStandardById(id);
        return JsonResult.ok(olderStaffStandardVO);
    }



    @GetMapping("/list")
    @ApiOperation(value="请求关联列表")
    @PreAuthorize("hasAuthority('staff/read')")
    @ApiOperationSupport(order = 400)
    public  JsonResult<List<OlderStaffListItemVO>> select(){
        log.debug("开始处理请求关联列表");
        List<OlderStaffListItemVO> list=olderStaffService.list();
        return JsonResult.ok(list);
    }


}
